<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="小区名称" prop="communityName">
        <el-input
          v-model="queryParams.communityName"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="停车场编号" prop="parkCode">
        <el-input
          v-model="queryParams.parkCode"
          placeholder="请输入停车场编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="停车场名称" prop="parkName">
        <el-input
          v-model="queryParams.parkName"
          placeholder="请输入停车场名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:park:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:park:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:park:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:park:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="停车场id" align="center" prop="id" v-if="false"/>
      <el-table-column label="停车场编号" align="center" prop="parkCode" />
      <el-table-column label="停车场名称" align="center" prop="parkName" width="150"/>
      <el-table-column label="小区名称" align="center" prop="communityName" />
      <el-table-column label="停车场地址" align="center" prop="address" width="130">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="300"
            trigger="hover"
            :disabled="scope.row.address.length <= 8"
          >
            <div>{{ scope.row.address }}</div>
            <span slot="reference" v-if="scope.row.address.length <= 10">{{scope.row.address}}</span>
            <span slot="reference" v-if="scope.row.address.length > 10">{{scope.row.address.substr(0, 8) + "..."}}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="停车位数" align="center" prop="slotsnumber" />
      <el-table-column label="临时车位" align="center" prop="tempCarports" />
      <el-table-column label="固定车位" align="center" prop="fixedCarports" />
      <el-table-column label="空车位数" align="center" prop="eptCarports" />
      <el-table-column label="免费时长(分钟)" align="center" prop="freeTime" />
      <el-table-column label="联系电话" align="center" prop="conCel" />
      <el-table-column label="停车场图片" align="center" prop="parkPicUrl" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-picture"
            v-if="scope.row.parkPicUrl != '' && scope.row.parkPicUrl != null "
            @click="showImg(scope.row.parkPicUrl)"
          >查看图片</el-button>
          <span v-else>无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:park:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:park:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停车场管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="150px">
        <el-form-item label="绑定小区" prop="communityId">
          <el-select v-model="form.communityId" style="width: 100%" placeholder="请选择小区绑定,若没有可先在小区管理添加相应区域" filterable clearable size="small">
            <el-option
              v-for="community in communityOptions"
              :key="community.id"
              :label="community.name"
              :value="Number(community.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="停车场编号" prop="parkCode">
          <el-input v-model="form.parkCode" placeholder="请输入停车场编号" />
        </el-form-item>
        <el-form-item label="停车场名称" prop="parkName">
          <el-input v-model="form.parkName" placeholder="请输入停车场名称" />
        </el-form-item>
        <el-form-item label="车场图片" prop="parkPicUrl">
          <upload-img v-model="form.parkPicUrl" :limit="9"></upload-img>
        </el-form-item>
        <el-form-item label="定位地址" prop="">
          <el-input v-model="locationAddress" disabled placeholder="请定位停车场地址" >
            <template #prepend>
                <el-button
                size="mini"
                type="primary"
                icon="el-icon-location"
                @click="handleLocation"
                >定位</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入停车场详细地址" />
        </el-form-item>
        <el-form-item label="小区名称" prop="area">
          <el-input v-model="form.area" placeholder="小区名称" />
        </el-form-item>
        <el-form-item label="状态" prop="type">
          <el-select v-model="form.type" placeholder="请选择状态">
            <el-option
              v-for="dict in roadOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出入口剩余车位数" prop="remainPostionNum">
          <el-input v-model="form.remainPostionNum" placeholder="请输入出入口剩余车位数" />
        </el-form-item>
        <el-form-item label="停车位数" prop="slotsnumber">
          <el-input v-model="form.slotsnumber" placeholder="请输入停车位数" />
        </el-form-item>
        <el-form-item label="引导系统剩余车位数" prop="mpgsRemainPostionNum">
          <el-input v-model="form.mpgsRemainPostionNum" placeholder="请输入引导系统剩余车位数" />
        </el-form-item>
        <el-form-item label="临时停车位数" prop="tempCarports">
          <el-input v-model="form.tempCarports" placeholder="请输入临时停车位数" />
        </el-form-item>
        <el-form-item label="固定车位数" prop="fixedCarports">
          <el-input v-model="form.fixedCarports" placeholder="请输入固定车位数" />
        </el-form-item>
        <el-form-item label="空车位数" prop="eptCarports">
          <el-input v-model="form.eptCarports" placeholder="请输入空车位数" />
        </el-form-item>

        <el-form-item label="缴费后离场时间(min)" prop="timeoutlength">
          <el-input v-model="form.timeoutlength" placeholder="请输入缴费后离场时间" />
        </el-form-item>

        <el-form-item label="是否上线智慧停车">
          <el-select v-model="form.validStatus" placeholder="是否上线智慧停车">
            <el-option
              v-for="dict in onLineOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="首次上线时间" prop="publishDate">
          <el-date-picker
            v-model="form.publishDate"
            type="date"
            placeholder="选择首次上线时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停车场标签" prop="flagTag">
          <el-input v-model="form.flagTag" placeholder="请输入停车场标签,方便查询车场信息" />
        </el-form-item>
        <el-form-item label="停车场类型" prop="parkType">
          <el-select v-model="form.parkType" placeholder="请选择停车场类型">
            <el-option
              v-for="dict in parkTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否连接平台">
          <el-select v-model="form.stopConnectPlatform" placeholder="是否连接平台">
            <el-option
              v-for="dict in stopConnectPlatformOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运营方负责人" prop="parkingSupervisor">
          <el-input v-model="form.parkingSupervisor" placeholder="请输入停车场运营方负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="parkingSupervisorPhone">
          <el-input v-model="form.parkingSupervisorPhone" placeholder="请输入停车场运营方负责人电话" />
        </el-form-item>
        <el-form-item label="适用公众号appId" prop="wechatAppids">
          <el-input v-model="form.wechatAppids" placeholder="请输入适用公众号appId" />
        </el-form-item>
        <el-form-item label="行政代码" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入停车场所在地区行政代码" />
        </el-form-item>
        <el-form-item label="是否免费停车场">
          <el-select v-model="form.isFree" placeholder="选择是否免费">
            <el-option
              v-for="dict in isFreeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴费规则图片" prop="parkChargeRulePicUrl" v-if="form.isFree == 1 ">
          <upload-img v-model="form.parkChargeRulePicUrl" :limit="1"></upload-img>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="入口数量" prop="inEntranceNum">
          <el-input v-model="form.inEntranceNum" placeholder="请输入入口数量" />
        </el-form-item>
        <el-form-item label="出口数量" prop="outEntraceNum">
          <el-input v-model="form.outEntraceNum" placeholder="请输入出口数量" />
        </el-form-item>
        <el-form-item label="免费停车时长(min)" prop="freeTime">
          <el-input v-model="form.freeTime" placeholder="请输入免费停车时长，单位分钟" />
        </el-form-item>
        <el-form-item label="联系电话" prop="conCel">
          <el-input v-model="form.conCel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="联系邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入联系邮箱" />
        </el-form-item>
        <el-form-item label="计费规则（单位：元/小时）" prop="toll">
          <el-input-number v-model="form.toll" placeholder="计费规则（单位：元/小时）" :min="0.01" :step="0.01"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 查看图片 -->
    <el-dialog :title="title" :visible.sync="openPhotos" v-if="openPhotos" width="60%" append-to-body>
      <show-imgs :imgs="photos"></show-imgs>
    </el-dialog>
    <!-- 打开地图 -->
    <div v-if="openMap" style="width: 60%">
      <TXMap @cancel="openMap = false" @map-confirm="confirmLocation" @getPlace="getPlace" :visible="openMap" :position="position"></TXMap>
    </div>
  </div>
</template>

<script>
import { listPark, getPark, delPark, addPark, updatePark, exportPark } from "@/api/system/park";
import TXMap from '@/components/TXMap';
import {jsonp} from "vue-jsonp";
import UploadImg from "@/components/UploadImg";
import ShowImgs from "@/components/ShowImgs";
import {getAllList} from "@/api/system/community";
export default {
  name: "Park",
  components: {
    ShowImgs,
    UploadImg,
    TXMap
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //图片框
      openPhotos: false,
      photos:"",
      // 总条数
      total: 0,
      // 停车场管理表格数据
      parkList: [],
      //小区下拉列表
      communityOptions: [],
      // 弹出层标题
      title: "",
      locationAddress: "",//定位地址
      // 是否显示弹出层
      open: false,
      openMap: false,
      // 是否绑定了黑盒子：0否 1是字典
      haveMqttOptions: [],
      //停车场是否上线智慧停车 0下线  1上线
      onLineOptions: [],
      // 停车场类型
      parkTypeOptions: [],
      //状态id,0路内 1路外
      roadOptions: [],
      longitudeLatitude:{},//地图经纬度
      // 是否连接平台
      stopConnectPlatformOptions: [],
      // 是否免费停车场 0免费 1需缴费
      isFreeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: undefined,
        communityName: undefined,
        parkCode: undefined,
        parkName: undefined,
      },
      // 表单参数
      form: {},
      //自动定位的经纬度
      position: this.getLatAndLng(),
      // 表单校验
      rules: {
        parkName: [
          {required: true, message: "停车场名称不能为空", trigger: "blur"}
        ],
        communityId: [
          {required: true, message: "请选择停车场所属小区或区域，若没有则在小区列表先添加所属区域", trigger: "blur"}
        ],
        parkCode: [
          {required: true, message: "停车场编号不能为空", trigger: "blur"}
        ],
        gpsx: [
          {required: true, message: "请定位停车场", trigger: "blur"}
        ],
        toll: [
          { required: true, message: "请填写计费规则（单位：元/小时）", trigger: "blur"}
        ],
        slotsnumber: [
          { required: true, message: "停车位数量不能为空", trigger: "blur"}
        ],
        freeTime: [
          { required: true, message: "请填写免费停车时长，单位：分钟", trigger: "blur"}
        ],
        parkingSupervisorPhone: [
          { message: "电话格式错误", trigger: "blur",pattern: /^1[3456789]\d{9}$/}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCommunityList();
    this.getDicts("park_have_mqtt").then(response => {
      this.haveMqttOptions = response.data;
    });
    this.getDicts("park_type").then(response => {
      this.parkTypeOptions = response.data;
    });
    this.getDicts("park_stop_connect_platform").then(response => {
      this.stopConnectPlatformOptions = response.data;
    });
    this.getDicts("park_is_free").then(response => {
      this.isFreeOptions = response.data;
    });
    this.getDicts("park_valid_status").then(response => {
      this.onLineOptions = response.data;
    });
    this.getDicts("park_status_type").then(response => {
      this.roadOptions = response.data;
    });
  },
  methods: {
    /** 查询停车场管理列表 */
    getList() {
      this.loading = true;
      listPark(this.queryParams).then(response => {
        this.parkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCommunityList(){
      let data={};
      getAllList(data).then(response => {
        this.communityOptions = response.data
      });
    },
    // 是否绑定了黑盒子：0否 1是字典翻译
    haveMqttFormat(row, column) {
      return this.selectDictLabel(this.haveMqttOptions, row.haveMqtt);
    },
    // 停车场类型字典翻译
    parkTypeFormat(row, column) {
      return this.selectDictLabel(this.parkTypeOptions, row.parkType);
    },
    // 是否连接平台字典翻译
    stopConnectPlatformFormat(row, column) {
      return this.selectDictLabel(this.stopConnectPlatformOptions, row.stopConnectPlatform);
    },
    // 是否免费停车场 0免费 1需缴费字典翻译
    isFreeFormat(row, column) {
      return this.selectDictLabel(this.isFreeOptions, row.isFree);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openPhotos = false;
      this.openMap = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        communityId: undefined,
        haveMqtt: undefined,
        parkCode: undefined,
        parkName: undefined,
        parkPicUrl: undefined,
        parkChargeRulePicUrl: undefined,
        address: undefined,
        slotsnumber: undefined,
        gpsx: undefined,
        gpsy: undefined,
        type: undefined,
        remainPostionNum: undefined,
        mpgsRemainPostionNum: undefined,
        area: undefined,
        timeoutlength: undefined,
        tempCarports: undefined,
        fixedCarports: undefined,
        validStatus: 0,
        publishDate: undefined,
        flagTag: undefined,
        parkType: undefined,
        stopConnectPlatform: 0,
        parkingSupervisor: undefined,
        parkingSupervisorPhone: undefined,
        wechatAppids: undefined,
        areaId: undefined,
        isFree: 0,
        description: undefined,
        inEntranceNum: undefined,
        outEntraceNum: undefined,
        freeTime: undefined,
        eptCarports: undefined,
        conCel: undefined,
        email: undefined,
        toll: undefined,
      };
      this.resetForm("form");
      this.locationAddress = "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 打开地图 */
    handleLocation() {
      this.openMap = true;
      this.title = "停车场定位";
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    confirmLocation() {
      const e = this.longitudeLatitude
      this.form.gpsy = parseFloat(e.lat).toFixed(6);
      this.form.gpsx = parseFloat(e.lng).toFixed(6);
      this.locationAddress = e.addr
      this.form.address = e.addr
      this.openMap = false
    },
    getPlace (e) {
      this.longitudeLatitude = e
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停车场管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getPark(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.form.toll = this.form.toll/100;
        this.position={
          latitude:this.form.gpsy,
          longitude:this.form.gpsx,
        }
        this.open = true;
        this.title = "修改停车场管理";
      });
    },
    /** 查看图片 */
    showImg(Img) {
      this.reset();
      this.openPhotos = true;
      if (Img){
        this.photos = Img
      }
      this.title = "停车场图片";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.toll = this.form.toll * 100;
          if (this.form.id != null) {
            updatePark(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.openMap = false;
              this.getList();
            });
          } else {
            addPark(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.openMap = false;
              this.getList();
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除停车场管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delPark(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有停车场管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPark(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

